import { fetch } from "@tauri-apps/plugin-http";
import _ from "lodash";

const request = async function (method, url, headers, params, body) {
  let headerDefault = {
    Accept: "application/json",
    "Content-Type": "application/json",
  };
  headers = _.merge(headerDefault, headers);

  if (params) {
    let searchParams = new URLSearchParams(params);
    url = `${url}?${searchParams}`;
  }
  console.log(method, url, headers, params, body);
  try {
    let options = {
      method: method,
      // mode: "cors",
      headers: headers,
      body: JSON.stringify(body),
    };
    const resp = await fetch(url, options);
    if (!resp.ok) {
      throw new Error(`Response status ${resp.status}`);
    }
    const json = await resp.json();
    return json;
  } catch (err) {
    console.log(err);
  }
  return {};
};
const get = async function (url, params) {
  return request("GET", url, null, params);
};


export {get}
export default request;
